#![allow(non_upper_case_globals, non_snake_case)]
use gl::types::*;
use gl_rs::utils::camera::Camera;
use gl_rs::utils::common::{load_texture, process_events, process_input};
use gl_rs::utils::shader::Shader;
use glfw::{Context, Glfw};

use cgmath::prelude::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3};
use std::os::raw::c_void;
use std::{mem, ptr};

const SCR_WIDTH: f32 = 960.;
const SCR_HEIGHT: f32 = 544.;

#[allow(non_snake_case)]
pub fn main_2_4_1() {
    let mut glfw: Glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
    glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3));
    glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core));
    #[cfg(target_os = "macos")]
    glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true));
    let (mut window, events) = glfw
        .create_window(
            SCR_WIDTH as u32,
            SCR_HEIGHT as u32,
            "LearnOpenGL",
            glfw::WindowMode::Windowed,
        )
        .expect("Failed to create GLFW window");
    window.make_current();
    window.set_framebuffer_size_polling(true);
    window.set_cursor_pos_polling(true);
    window.set_scroll_polling(true);
    window.set_cursor_mode(glfw::CursorMode::Disabled);

    gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);

    let mut camera = Camera {
        position: Point3::new(0., 0., 3.),
        ..Camera::default()
    };

    let mut delta_time: f32;
    let mut last_frame: f32 = 0.;
    let mut first_mouse = true;
    let mut last_x = SCR_WIDTH / 2.;
    let mut last_y = SCR_HEIGHT / 2.;

    let light_position: Vector3<f32> = vec3(1.2, 1., 2.);

    let (lighting_shader, lamp_shader, VBO, cubeVAO, lightVAO, diffuse_map) = unsafe {
        gl::Enable(gl::DEPTH_TEST);

        let lighting_shader: Shader = Shader::new(
            "src/_2_lighting/shaders/4.1.lighting_maps.vert",
            "src/_2_lighting/shaders/4.1.lighting_maps.frag",
        );
        let lamp_shader: Shader = Shader::new(
            "src/_2_lighting/shaders/1.1.lamp.vert",
            "src/_2_lighting/shaders/1.1.lamp.frag",
        );

        #[cfg_attr(rustfmt, rustfmt_skip)]
        let vertices: [f32; 288] = [
            -0.5, -0.5, -0.5,  0.0,  0.0, -1.0,  0.0,  0.0,
             0.5, -0.5, -0.5,  0.0,  0.0, -1.0,  1.0,  0.0,
             0.5,  0.5, -0.5,  0.0,  0.0, -1.0,  1.0,  1.0,
             0.5,  0.5, -0.5,  0.0,  0.0, -1.0,  1.0,  1.0,
            -0.5,  0.5, -0.5,  0.0,  0.0, -1.0,  0.0,  1.0,
            -0.5, -0.5, -0.5,  0.0,  0.0, -1.0,  0.0,  0.0,

            -0.5, -0.5,  0.5,  0.0,  0.0,  1.0,  0.0,  0.0,
             0.5, -0.5,  0.5,  0.0,  0.0,  1.0,  1.0,  0.0,
             0.5,  0.5,  0.5,  0.0,  0.0,  1.0,  1.0,  1.0,
             0.5,  0.5,  0.5,  0.0,  0.0,  1.0,  1.0,  1.0,
            -0.5,  0.5,  0.5,  0.0,  0.0,  1.0,  0.0,  1.0,
            -0.5, -0.5,  0.5,  0.0,  0.0,  1.0,  0.0,  0.0,

            -0.5,  0.5,  0.5, -1.0,  0.0,  0.0,  1.0,  0.0,
            -0.5,  0.5, -0.5, -1.0,  0.0,  0.0,  1.0,  1.0,
            -0.5, -0.5, -0.5, -1.0,  0.0,  0.0,  0.0,  1.0,
            -0.5, -0.5, -0.5, -1.0,  0.0,  0.0,  0.0,  1.0,
            -0.5, -0.5,  0.5, -1.0,  0.0,  0.0,  0.0,  0.0,
            -0.5,  0.5,  0.5, -1.0,  0.0,  0.0,  1.0,  0.0,

             0.5,  0.5,  0.5,  1.0,  0.0,  0.0,  1.0,  0.0,
             0.5,  0.5, -0.5,  1.0,  0.0,  0.0,  1.0,  1.0,
             0.5, -0.5, -0.5,  1.0,  0.0,  0.0,  0.0,  1.0,
             0.5, -0.5, -0.5,  1.0,  0.0,  0.0,  0.0,  1.0,
             0.5, -0.5,  0.5,  1.0,  0.0,  0.0,  0.0,  0.0,
             0.5,  0.5,  0.5,  1.0,  0.0,  0.0,  1.0,  0.0,

            -0.5, -0.5, -0.5,  0.0, -1.0,  0.0,  0.0,  1.0,
             0.5, -0.5, -0.5,  0.0, -1.0,  0.0,  1.0,  1.0,
             0.5, -0.5,  0.5,  0.0, -1.0,  0.0,  1.0,  0.0,
             0.5, -0.5,  0.5,  0.0, -1.0,  0.0,  1.0,  0.0,
            -0.5, -0.5,  0.5,  0.0, -1.0,  0.0,  0.0,  0.0,
            -0.5, -0.5, -0.5,  0.0, -1.0,  0.0,  0.0,  1.0,

            -0.5,  0.5, -0.5,  0.0,  1.0,  0.0,  0.0,  1.0,
             0.5,  0.5, -0.5,  0.0,  1.0,  0.0,  1.0,  1.0,
             0.5,  0.5,  0.5,  0.0,  1.0,  0.0,  1.0,  0.0,
             0.5,  0.5,  0.5,  0.0,  1.0,  0.0,  1.0,  0.0,
            -0.5,  0.5,  0.5,  0.0,  1.0,  0.0,  0.0,  0.0,
            -0.5,  0.5, -0.5,  0.0,  1.0,  0.0,  0.0,  1.0
        ];

        let (mut VBO, mut cubeVAO, mut lightVAO) = (0, 0, 0);
        gl::GenVertexArrays(1, &mut cubeVAO);
        gl::GenBuffers(1, &mut VBO);

        gl::BindBuffer(gl::ARRAY_BUFFER, VBO);
        gl::BufferData(
            gl::ARRAY_BUFFER,
            (vertices.len() * mem::size_of::<GLfloat>()) as GLsizeiptr,
            &vertices[0] as *const f32 as *const c_void,
            gl::STATIC_DRAW,
        );
        gl::BindVertexArray(cubeVAO);
        let stride = 8 * mem::size_of::<GLfloat>() as GLsizei;
        gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
        gl::EnableVertexAttribArray(0);

        gl::VertexAttribPointer(
            1,
            3,
            gl::FLOAT,
            gl::FALSE,
            stride,
            (3 * mem::size_of::<GLfloat>()) as *const c_void,
        );
        gl::EnableVertexAttribArray(1);
        gl::VertexAttribPointer(
            2,
            2,
            gl::FLOAT,
            gl::FALSE,
            stride,
            (6 * mem::size_of::<GLfloat>()) as *const c_void,
        );
        gl::EnableVertexAttribArray(2);

        gl::GenVertexArrays(1, &mut lightVAO);
        gl::BindVertexArray(lightVAO);

        gl::BindBuffer(gl::ARRAY_BUFFER, VBO);
        gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, stride, ptr::null());
        gl::EnableVertexAttribArray(0);

        let diffuse_map = load_texture("resources/textures/container2.png");

        lighting_shader.use_program();
        lighting_shader.set_int(c_str!("material.diffuse"), 0);

        (lighting_shader, lamp_shader, VBO, cubeVAO, lightVAO, diffuse_map)
    };
    while !window.should_close() {
        let current_frame = glfw.get_time() as f32;
        delta_time = current_frame - last_frame;
        last_frame = current_frame;

        process_events(&events, &mut first_mouse, &mut last_x, &mut last_y, &mut camera);
        process_input(&mut window, delta_time, &mut camera);

        unsafe {
            gl::ClearColor(0.1, 0.1, 0.1, 1.);
            gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);

            lighting_shader.use_program();
            lighting_shader.set_vector3(c_str!("light.position"), &light_position);
            lighting_shader.set_vector3(c_str!("viewPos"), &camera.position.to_vec());

            lighting_shader.set_vec3(c_str!("light.ambient"), 0.2, 0.2, 0.2);
            lighting_shader.set_vec3(c_str!("light.diffuse"), 0.5, 0.5, 0.5);
            lighting_shader.set_vec3(c_str!("light.specular"), 1., 1., 1.);

            lighting_shader.set_vec3(c_str!("material.specular"), 0.5, 0.5, 0.5);
            lighting_shader.set_float(c_str!("material.shininess"), 64.);

            let projection: Matrix4<f32> = perspective(Deg(camera.zoom), SCR_WIDTH / SCR_HEIGHT, 0.1, 100.);
            let view: Matrix4<f32> = camera.get_view_matrix();

            lighting_shader.set_mat4(c_str!("projection"), &projection);
            lighting_shader.set_mat4(c_str!("view"), &view);

            let mut model: Matrix4<f32> = Matrix4::<f32>::identity();
            lighting_shader.set_mat4(c_str!("model"), &model);

            gl::ActiveTexture(gl::TEXTURE0);
            gl::BindTexture(gl::TEXTURE_2D, diffuse_map);

            gl::BindVertexArray(cubeVAO);
            gl::DrawArrays(gl::TRIANGLES, 0, 36);

            lamp_shader.use_program();

            lamp_shader.set_mat4(c_str!("projection"), &projection);
            lamp_shader.set_mat4(c_str!("view"), &view);
            model = Matrix4::from_translation(light_position);
            model = model * Matrix4::from_scale(0.2);
            lamp_shader.set_mat4(c_str!("model"), &model);

            gl::BindVertexArray(lightVAO);
            gl::DrawArrays(gl::TRIANGLES, 0, 36);
        }
        window.swap_buffers();
        glfw.poll_events();
    }
    unsafe {
        gl::DeleteVertexArrays(1, &cubeVAO);
        gl::DeleteVertexArrays(1, &lightVAO);
        gl::DeleteBuffers(1, &VBO);
    }
}
